layui.use(["laytp"], function () {
    const funController = {};
    //静态页面地址前缀
    window.htmlPrefix = facade.compatibleHtmlPath("/admin/ah/candidate_outfit/");
    //后端接口地址前缀
    window.apiPrefix  = facade.compatibleApiRoute("/admin.ah.CandidateOutfit/");
    //表格渲染
    funController.tableRender = function (where, page) {
        if(typeof where =="undefined"){
            where = {
                order_param :{
                    'type' :'desc',
                    'field' :'sort',
                }
            }
        }
        layui.table.render({
            elem: "#laytp-table"
            , id: "laytp-table"
            , url: window.apiPrefix+"index"
            , toolbar: "#default-toolbar"
            , defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            , where: where
            , method: "GET"
            , cellMinWidth: 120
            , skin: 'line'
            , loading: false
            , page: {
                curr: page
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return facade.parseTableData(res, true);
            }
            , done: function(){
                layui.laytpTable.done();
            }
            , cols: [[
                {type:'checkbox',fixed:'left'}
                , {field:'id',title:'ID',align:'center',minWidth:120,fixed:'left'}
                , {field:'name',title:'配装名称',align:'center',minWidth:120,fixed:'left'}
                , {field: "sort", title: "排序", minWidth: 80, align: "center",fixed:'left', templet:function(d){
                        return laytpForm.tableForm.editInput('sort',d, window.apiPrefix+'setSort');
                    }}
                , {field: "likes_number", title: "点赞数", minWidth: 80, align: "center",fixed:'left', templet:function(d){
                        return laytpForm.tableForm.editInput('likes_number',d, window.apiPrefix+'setNumber');
                    }}

                , {field:'role_name',title:'职业',align:'center',minWidth:80,fixed:'left'}
                , {field:'user_id',title:'用户ID',align:'center',minWidth:120}
                , {field:'user_name',title:'用户名称',align:'center',minWidth:120}
                , {field:'create_time',title:'创建时间',align:'center',minWidth:172}
                , {field:'update_time',title:'更新时间',align:'center',minWidth:172}
                , {field:'publish_label',title:'发布状态',align:'center',minWidth:120}
                ,{
                    field: "operation",
                    title: "操作",
                    toolbar: "#default-bar",
                    fixed: "right",
                    align: "center",
                    minWidth: 240
                }
            ]]
        });

        //监听数据表格顶部左侧按钮点击事件
        layui.table.on("toolbar(laytp-table)", function (obj) {
            //默认按钮点击事件，包括添加按钮和回收站按钮
            var defaultTableToolbar = layui.context.get("defaultTableToolbar");
            if (defaultTableToolbar.indexOf(obj.event) !== -1) {
                laytp.tableToolbar(obj);
            } else {
                // //自定义按钮点击事件
                // switch(obj.event){
                // //自定义按钮点击事件
                // case "":
                //
                //     break;
                // }
            }
        });

        //监听数据表格[操作列]按钮点击事件
        layui.table.on("tool(laytp-table)", function (obj) {
            var defaultTableTool = layui.context.get("defaultTableTool");
            if (defaultTableTool.indexOf(obj.event) !== -1) {
                laytp.tableTool(obj);
            } else {
                let data = {}
                data.id = obj.data.id;
                //自定义按钮点击事件
                switch(obj.event){
                //自定义按钮点击事件
                    case "publish_yes":
                        data.type = 'yes';
                        facade.popupConfirm({
                            text: "确认发布这套配装吗？",
                            route: window.apiPrefix + "publish",
                            data: data
                        }, function(){
                            layui.table.reload("laytp-table");
                            parent.parent.renderMenu();//重新渲染菜单
                        });
                        break;
                    case "publish_no":
                        data.type = 'no';
                        facade.popupConfirm({
                            text: "确认取消发布这套配装吗？",
                            route: window.apiPrefix + "publish",
                            data:data
                        }, function(){
                            layui.table.reload("laytp-table");
                            parent.parent.renderMenu();//重新渲染菜单
                        });
                        break;
                    case "recommend_yes":
                        data.type = 'yes';
                        facade.popupDiv({
                            title: "推荐配装",
                            path:"/admin/ah/candidate_outfit/label.html?id="+data.id,
                            width:'30%',
                            height:'40%',
                        }, function(){
                            layui.table.reload("laytp-table");
                            parent.parent.renderMenu();//重新渲染菜单
                        });
                        break;
                }
            }
        });

        //监听表头排序事件
        layui.table.on('sort(laytp-table)', function(obj){
            layui.table.reload('laytp-table', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                , where: {
                    "order_param" : {
                        "field" : obj.field,
                        "type" : obj.type
                    }
                }
            });
        });
    };

    funController.tableRender();

    window.funController = funController;
});